package com.xuecheng.media.controller;

import com.xuecheng.api.media.MediaApi;
import com.xuecheng.api.media.model.aliyun.VodUploadRequest;
import com.xuecheng.api.media.model.aliyun.VodUploadToken;
import com.xuecheng.api.media.model.dto.MediaDTO;
import com.xuecheng.api.media.model.qo.QueryMediaModel;
import com.xuecheng.api.media.model.vo.MediaVO;
import com.xuecheng.api.media.model.vo.ResourceUrlModel;
import com.xuecheng.common.domain.page.PageRequestParams;
import com.xuecheng.common.domain.page.PageVO;
import com.xuecheng.common.domain.response.RestResponse;
import com.xuecheng.media.common.utils.UAASecurityUtil;
import com.xuecheng.media.convert.MediaConvert;
import com.xuecheng.media.service.MediaService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 媒资信息 前端控制器
 * </p>
 *
 * @author itcast
 */
@Slf4j
@RestController
public class MediaController implements MediaApi {

    @Autowired
    private MediaService mediaService;

    /**
     * 获取上传凭证数据
     *
     * @param vodUploadRequest 请求
     * @return 上传凭证
     */
    @PostMapping("media/vod-token")
    public VodUploadToken createUploadToken(@RequestBody VodUploadRequest vodUploadRequest) {
        return this.mediaService.createUploadToken(vodUploadRequest);
    }

    /**
     * 刷新上传凭证
     *
     * @param videoId 视频id
     * @return 刷新后的上传凭证
     */
    @GetMapping("media/refresh-vod-token/{videoId}")
    public VodUploadToken refreshUploadToken(@PathVariable String videoId) {
        return this.mediaService.refreshDUploadToken(videoId);
    }

    /**
     * 保存媒资
     *
     * @param vo 媒资信息
     * @return 保存后的媒资信息
     */
    @PostMapping("media")
    public MediaDTO saveMedia(@RequestBody MediaVO vo) {
        //获取机构id
        Long companyId = UAASecurityUtil.getCompanyId();
        MediaDTO mediaDTO = MediaConvert.INSTANCE.vo2dto(vo);
        mediaDTO.setCompanyId(companyId);
        return this.mediaService.saveMedia(mediaDTO);
    }

    /**
     * 分页查询媒资信息
     *
     * @param params 分页查询参数
     * @param model  条件查询参数
     * @return 分页查询结果对象
     */
    @PostMapping("media/list")
    public PageVO queryMediaList(PageRequestParams params, @RequestBody QueryMediaModel model) {
        //获取机构id
        Long companyId = UAASecurityUtil.getCompanyId();
        return this.mediaService.queryMediaList(params, model, companyId);
    }

    /**
     * 预览媒资信息
     *
     * @param mediaId 媒资id
     * @return 媒资播放地址
     */
    @GetMapping("media/preview/{mediaId}")
    public Object previewMedia(@PathVariable Long mediaId) {
        //获取机构id
        Long companyId = UAASecurityUtil.getCompanyId();
        return this.mediaService.previewMedia(mediaId, companyId);
    }

    /**
     * 根据id删除媒资信息
     *
     * @param mediaId 媒资id
     */
    @DeleteMapping("media/{mediaId}")
    public void deleteMedia(@PathVariable Long mediaId) {
        //获取机构id
        Long companyId = UAASecurityUtil.getCompanyId();
        this.mediaService.deleteMedia(mediaId, companyId);
    }


    /**
     * 根据id查询媒资信息
     *
     * @param mediaId 媒资id
     */
    @GetMapping("l/media/{mediaId}")
    public RestResponse<MediaDTO> getMediaById4s(@PathVariable Long mediaId) {
        return this.mediaService.getMediaById4s(mediaId);
    }

    @Override
    @GetMapping("l/media-url/{mediaId}")
    public RestResponse<ResourceUrlModel> getMediaUrl(@PathVariable Long mediaId) {
        RestResponse<ResourceUrlModel> response = this.mediaService.getMediaUrl(mediaId);
        return response;
    }
}
